package org.qiancizhan.Utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5CryptoUtil {
    public static String encrypt(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1)
                    hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5算法不存在", e);
        }
    }

    public static boolean verify(String input, String md5Hash) {
        if (input == null || md5Hash == null) return false;
        String encryptedInput = encrypt(input);
        return encryptedInput.equalsIgnoreCase(md5Hash);
    }
}
